package cn.itsource.crm.web.controller;

import cn.itsource.crm.domain.Warranty;
import cn.itsource.crm.domain.WarrantyDetails;
import cn.itsource.crm.query.WarrantyDetailsQuery;
import cn.itsource.crm.query.WarrantyQuery;
import cn.itsource.crm.service.IWarrantyDetailsService;
import cn.itsource.crm.service.IWarrantyService;
import cn.itsource.util.AjaxResultZBZ;
import cn.itsource.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@Controller
@RequestMapping("/warrantyDetails")
@CrossOrigin
public class WarrantyDetailsController {

    @Autowired
    private IWarrantyDetailsService warrantyDetailsService;

    /**
     * equestMethod.PATCH   批量操作
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.PATCH)
    @ResponseBody
    public List<WarrantyDetails> findAll() {
        return warrantyDetailsService.findAll();
    }

    /**
     * 查询分页数据
     *
     * @param query
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.PATCH)
    @ResponseBody
    public PageList<WarrantyDetails> queryPage(@RequestBody WarrantyDetailsQuery query) {
        return warrantyDetailsService.queryPage(query);
    }

    /**
     * RequestMethod.GET  表示查询一个
     *
     * @param id
     * @return
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public WarrantyDetails findOne(@PathVariable Long id) {
        return warrantyDetailsService.findOne(id);
    }

    /**
     * RequestMethod.PUT 表示新增请求
     *
     * @param warrantyDetails
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public AjaxResultZBZ add(@RequestBody WarrantyDetails warrantyDetails) {
        try {
            warrantyDetailsService.save(warrantyDetails);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResultZBZ("新增失败:" + e.getMessage());
        }
        return new AjaxResultZBZ();
    }

    /**
     * RequestMethod.POST 修改请求
     *
     * @param warrantyDetails
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public AjaxResultZBZ update(@RequestBody WarrantyDetails warrantyDetails) {
        try {
            warrantyDetailsService.update(warrantyDetails);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResultZBZ("修改失败:" + e.getMessage());
        }
        return new AjaxResultZBZ();
    }

    /**
     * RequestMethod.DELETE  删除请求
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxResultZBZ delete(@PathVariable Long id) {
        try {
            warrantyDetailsService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResultZBZ("删除失败:" + e.getMessage());
        }
        return new AjaxResultZBZ();
    }

/**
 * RequestMethod.DELETE  批量删除请求
 * @return
 */

   /* @RequestMapping(value = "/deleteMany",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResultZBZ deleteMany(@RequestBody WarrantyQuery warrantyQuery){
        try {
            System.out.println(warrantyQuery.getIds());
            warrantyDetailsService.deleteMany(warrantyQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResultZBZ("批量删除失败:"+e.getMessage());
        }
        return new AjaxResultZBZ();
    }*/

    @RequestMapping(value = "/details", method = RequestMethod.PATCH)
    @ResponseBody
    public List<WarrantyDetails> findDetails(@RequestBody WarrantyDetailsQuery detailsQuery) {
        Long warrantyID = detailsQuery.getWarrantyID();
        return warrantyDetailsService.findDetails(warrantyID);
    }
}